<template>
  <div class="container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  name: "Rank",
  data() {
    return {
      chartInstance: null,
      allData: null,
      startValue: 0,
      endValue: 9,
      timerId: null,
    }
  },
  created() {
    this.$socket.registerCallBack('rankData', this.getDate)
  },
  mounted() {
    this.initChart()
    // this.getDate()
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: ''
    })
    window.addEventListener('resize', this.screenAdapter)
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('rankData', this.getData)
    clearInterval(this.timerId)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      const initOptions = {
        title: {
          text: '地区销售排行',
          left: 20,
          top: 20
        },
        grid: {
          containLabel: true
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: 'category',
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'bar',
            // barWidth: 50,
            itemStyle: {
              barBorderRadius: [50, 50, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(initOptions)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    getDate(resData) {
      this.allData = resData
      // this.allData.sort((a, b) => {
      //   return b.value - a.value
      // })
      this.updateChart()
    },
    updateChart() {
      const xData = this.allData.map(item => {
        return item.name
      })
      const yData = this.allData.map(item => {
        return item.value
      })
      const dataOptions = {
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        xAxis: {
          data: xData
        },
        series: [
          {
            data: yData,
            itemStyle: {
              color: (arg) => {
                if (arg.value > 300) {
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#3f73ae'
                    },
                    {
                      offset: 1,
                      color: '#23e0e4'
                    }
                  ])
                } else if (arg.value > 200) {
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#8ec5fc'
                    },
                    {
                      offset: 1,
                      color: '#e0c3fc'
                    }
                  ])
                } else {
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: '#764ba2'
                    },
                    {
                      offset: 1,
                      color: '#667eea'
                    }
                  ])
                }
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(dataOptions)
    },
    screenAdapter() {
      const titleFontSize = this.$refs.rank_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval() {
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.updateChart()
      }, 2000)
    }
  }
}
</script>

<style scoped>

</style>